package com.shzu.jwc.ty.service;

import com.shzu.jwc.ty.bean.PeExamination;
import com.shzu.jwc.ty.bean.PeExaminationExample;
import com.shzu.jwc.ty.bean.PeExaminationTeacher;
import com.shzu.jwc.ty.bean.PeExaminationTeacherExample;
import com.shzu.jwc.ty.common.Page;
import com.shzu.jwc.ty.common.ReadExcelPeEx;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.dao.PeExaminationMapper;
import com.shzu.jwc.ty.dao.PeExaminationTeacherMapper;
import com.shzu.jwc.ty.param.StartandEndDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service
public class PeExaminationservice {
	
	@Autowired
	PeExaminationMapper examinationMapper;
	
	@Autowired
	SystemYearservice systemYearservice;

	@Autowired
	PeExaminationTeacherMapper peExaminationTeacherMapper;

	@Autowired
	PeExaminationScoreservice PeExaminationScoreservice;
	
	public List<PeExamination> getexamination(Page page,String peExaminationCode)
	{
		return examinationMapper.getexamination(null,0,systemYearservice.getoneyearby(Semester.getnow()),peExaminationCode,page.getStartPos(),page.getPageSize());
	}

	public List<PeExamination> getskilltest(String peExaminationCode){
		Integer systemYearId=systemYearservice.getoneyearby(Semester.getnow());
		PeExaminationExample examinationExample=new PeExaminationExample();
		PeExaminationExample.Criteria criteria=examinationExample.createCriteria();
		criteria.andSystemYearIdEqualTo(systemYearId);
		criteria.andPeExaminationStateEqualTo(0);
		criteria.andPeExaminationCodeEqualTo(peExaminationCode);
		return examinationMapper.selectByExample(examinationExample);
	}

	public Integer countselectexamination(Integer systemYearId,String peExaminationCode){
		return (int) examinationMapper.countexamination(null,0,systemYearId,peExaminationCode);
	}

	public Integer countexamination(String peExaminationCode){
		return (int) examinationMapper.countexamination(null,0,systemYearservice.getoneyearby(Semester.getnow()),peExaminationCode);
	}
	
	public Integer insertPeExamination(PeExamination examination)
	{
		Integer systemYearId=systemYearservice.getoneyearby(Semester.getnow());
		examination.setSystemYearId(systemYearId);
		examination.setPeExaminationState(0);
		return examinationMapper.insertSelective(examination);
	}
	
	public Integer delete(Integer peExaminationId)
	{
		PeExamination peExamination=new PeExamination();
		peExamination.setPeExaminationId(peExaminationId);
		peExamination.setPeExaminationState(3);
		examinationMapper.updateByPrimaryKeySelective(peExamination);
		return PeExaminationScoreservice.updatestatusbyexamination(3,peExaminationId);
	}
	
	public PeExamination getone(Integer peExaminationId) 
	{
		return examinationMapper.selectByPrimaryKey(peExaminationId);
	}
	
	public Integer update(PeExamination peExamination)
	{
		return examinationMapper.updateByPrimaryKeySelective(peExamination);
	}
	
	public List<PeExamination> search(String peExaminationName,String peExaminationCode)
	{
		PeExaminationExample examinationExample=new PeExaminationExample();
		PeExaminationExample.Criteria criteria=examinationExample.createCriteria();
		criteria.andPeExaminationStateEqualTo(0);
		criteria.andPeExaminationCodeEqualTo(peExaminationCode);
		criteria.andPeExaminationNameLike("%"+peExaminationName+"%");
		return examinationMapper.selectByExample(examinationExample);
	}

	public List<PeExamination> bydatesearch(Integer syetemYearId,Page page,String peExaminationCode)
	{
		return examinationMapper.getexamination(null,0,syetemYearId,peExaminationCode,page.getStartPos(),page.getPageSize());
	}

	public Integer countweekexamination(Integer systemExamWeeklyId,String peExaminationCode){
		return (int) examinationMapper.countexamination(systemExamWeeklyId,0,systemYearservice.getoneyearby(Semester.getnow()),peExaminationCode);
	}

	public List<PeExamination> selectweek(Integer systemExamWeeklyId,Page page,String peExaminationCode){
		return examinationMapper.getexamination(systemExamWeeklyId,0,systemYearservice.getoneyearby(Semester.getnow()),peExaminationCode,page.getStartPos(),page.getPageSize());
	}

	public Integer copyone(Integer peExaminationId){
		PeExamination peExamination=examinationMapper.selectByPrimaryKey(peExaminationId);
		peExamination.setPeExaminationId(null);
		peExamination.setPeExaminationAddTime(null);
		return examinationMapper.insertSelective(peExamination);
	}

	public List<PeExaminationTeacher> showpint(String peExaminationCode){
		PeExaminationTeacherExample example=new PeExaminationTeacherExample();
		PeExaminationTeacherExample.Criteria criteria=example.createCriteria();
		criteria.andPeExaminationCodeEqualTo(peExaminationCode);
		return peExaminationTeacherMapper.selectByExample(example);
	}

	public List<PeExaminationTeacher> selectshowpint(StartandEndDate startandEndDate,String peExaminationCode){
        return peExaminationTeacherMapper.showprint(startandEndDate,peExaminationCode);
    }


    public void importPe(String name, MultipartFile file,String peExaminationCode){
		ReadExcelPeEx excelPeEx=new ReadExcelPeEx();
		List<PeExamination> peExaminations=excelPeEx.getExcelInfo(name,file);
		for(int i=0;i<peExaminations.size();i++){
			peExaminations.get(i).setPeExaminationState(0);
			peExaminations.get(i).setSystemYearId(systemYearservice.getoneyearby(Semester.getnow()));
			peExaminations.get(i).setPeExaminationCode(peExaminationCode);
			examinationMapper.insert(peExaminations.get(i));
		}
		System.out.println(excelPeEx.getErrorInfo());
	}

}
